/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.site.console.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.cud.VideoFileCUDService;
import com.us.usws.service.ro.VideoFileROService;
import com.us.usws.support.uitls.DataPageUtil;
import com.us.usws.support.uitls.FilterDto;
import com.us.usws.support.uitls.FilterModel;
import com.us.usws.support.uitls.JSONDataUtil;
import com.us.usws.support.uitls.Order;
import com.us.usws.support.uitls.SiteWebContext;
import com.us.usws.support.uitls.WebContext;

/**
 * Struts2 action
 * @author robot
 */
public class VideoFileAction extends BasePageSearchAction<VideoFile> {

	private static final long serialVersionUID = 1L;

	private VideoFile videoFile;

	@Inject
	private VideoFileROService videoFileROService;

	@Inject
	private VideoFileCUDService videoFileCUDService;

	private String uids;

	/**
	 * ajax load the list of VideoFile
	 */
	public void ajaxLoadVideoFiles() {
		createDataPageUtil();
		dataPageUtil.setOrder(Order.desc("create_date"));
		if(videoFile != null) {
			Map<FilterDto, Object> filter = new HashMap<FilterDto, Object>(); 
			
			String mimeType = videoFile.getMimeType();
			if(StringUtils.isNotBlank(mimeType)) {
				filter.put(new FilterDto("mimeType", FilterModel.LIKE), mimeType);
			}
			if(filter.size() > 0) {
				dataPageUtil.setFilter(filter);
			}
		}
		videoFileROService.loadVideoFilesByPage(dataPageUtil);
		getOut().println(JSONDataUtil.extJsonSupportPage(dataPageUtil.getResult(), dataPageUtil.getRowCount()));	}
	
	public void ajaxLoadVideoFile() {
		getOut().println(JSONDataUtil.jsonStringfromObject(videoFileROService.getVideoFile(videoFile.getUid())));
	}

	/**
	 * ajax update VideoFile
	 */
	public void ajaxUpdateVideoFile() {
		getResponse().setContentType("text/html;charset=UTF-8");
		if (videoFile != null) {
			String path = getRealPath();
			Date now = new Date(System.currentTimeMillis());
			WebContext wc = new SiteWebContext(getRequest(), getResponse());
			DOUser user = wc.getUser();
			
			String disPlayLen = videoFile.getDisPlayLen();
			if(StringUtils.isNotBlank(disPlayLen)) {
				String[] lens = StringUtils.split(disPlayLen, ":");
				videoFile.setPlayLen((Integer.parseInt(lens[0])*60*60+Integer.parseInt(lens[1])*60 + Integer.parseInt(lens[2]))*1000L);
			}
			
			String uid = videoFile.getUid();
			if (StringUtils.isNotBlank(uid)) {
				
				videoFile.setLastUpdByUid(user.getUid());
				videoFile.setLastUpdDate(now);
				
				String snapshot = videoFile.getSnapshot();
				if(StringUtils.isNotBlank(snapshot) && snapshot.contains("/")) {
					videoFile.setSnapshot(path + snapshot);
				}
				
				String videoPath = videoFile.getVideoPath();
				if(StringUtils.isNotBlank(videoPath) && videoPath.contains("/")) {
					videoFile.setVideoPath(path + videoPath);
				}
				
				videoFileCUDService.updateVideoFile(videoFile);
			} else {
				videoFile.setCreateByUid(user.getUid());
				videoFile.setCreateDate(now);
				String videoPath = videoFile.getVideoPath();
				String snapshot = videoFile.getSnapshot();
				if(StringUtils.isNotBlank(snapshot)) {
					videoFile.setSnapshot(path + snapshot);
				}
				
				if(StringUtils.isNotBlank(videoPath)) {
					videoFile.setVideoPath(path + videoPath);
				}
				
				videoFileCUDService.addVideoFile(videoFile);
			}
			getOut().print(true);
		} else {
			getOut().print(false);
		}
	}

	/**
	 * ajax delete user.
	 */
	public void ajaxDeleteVideoFile() {
		List<String> fileUids = videoFileCUDService.deleteVideoFileMore(uids);
		getOut().print(JSONDataUtil.jsonStringfromObject(fileUids));
	}
	
	public void setVideoFile(VideoFile videoFile) {
		this.videoFile = videoFile;
	}

	public VideoFile getVideoFile() {
		return videoFile;
	}

	public DataPageUtil<VideoFile> getDataPageUtil() {
		return dataPageUtil;
	}

	public void setDataPageUtil(DataPageUtil<VideoFile> dataPageUtil) {
		this.dataPageUtil = dataPageUtil;
	}

	public String getUids() {
		return uids;
	}

	public void setUids(String uids) {
		this.uids = uids;
	}

}